# SubPython main application file

import wx
import wx.stc as stc
import wx.aui as aui
import os
import array
import shutil
import keyword
import sys
from xml.sax import make_parser, SAXException
from xml.sax.handler import ContentHandler		
			
import IDE_texteditor
import IDE_projectmanager
##import IDE_projectreader
##import IDE_autodownload
import IDE_menumanager
##import IDE_dialog_gettingstarted
##import IDE_customstatusbar
##import IDE_worldeditor
##import IDE_taskmanager

class MainWindow(wx.Frame):
	def __init__(self, parent, id, title):
		wx.Frame.__init__(self, parent, id, title, size=wx.Size(800,600))

		self.Maximize()
		self.appname = sys.argv[0]
		self.appdir = os.path.dirname(sys.argv[0])

		##Init
		self.last_name_saved = 'Empty'
		self.modified = False
		self.currentpagesopen = 0

		# Create all our objects
		self._mgr = wx.aui.AuiManager()
		self._mgr.SetManagedWindow(self)
		self.projectman = IDE_projectmanager.ProjectManager(self, -1, self)
		##self.handler = IDE_projectreader.ProjectXMLHandler(self,self.projectman)
		self.subversionlog = wx.TextCtrl(self, -1,
						"",
						style=wx.TE_MULTILINE|wx.TE_PROCESS_ENTER)
		self.subversionlog.SetEditable(False)
		self.SubversionLog("Please load or create a new project.")
		self.notebook = aui.AuiNotebook(self, -1)

		# Setup the working environment
		self._mgr.AddPane(self.projectman, wx.aui.AuiPaneInfo().BestSize(wx.Size(200,600)).Name("Project Manager").Caption("Project Manager").Left().Layer(1).Position(1).CloseButton(False).MaximizeButton(True))
		self._mgr.AddPane(self.subversionlog, wx.aui.AuiPaneInfo().BestSize(wx.Size(200,150)).Name("Subversion Log").Caption("Subversion Log").Bottom().Layer(1).Position(1).CloseButton(False).MaximizeButton(True))
		self._mgr.AddPane(self.notebook, wx.aui.AuiPaneInfo().Name("Notebook").CenterPane())

		# Bind events
		self.Bind(wx.EVT_CLOSE, self.OnQuitX, self)
		self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.projectman.OnActivate, self.projectman)
		self.Bind(wx.aui.EVT_AUINOTEBOOK_PAGE_CLOSE, self.projectman.OnPageClose, self.notebook)

		# Add our menu manager (handles all the menu items)
		self.menumanager = IDE_menumanager.MenuManager(self, self.projectman)
		
		# Getting started
		##self.gettingstarteddlg = IDE_dialog_gettingstarted.GettingStartingDialog(self)
		##wx.CallLater(100, self.gettingstarteddlg.ShowModal)

		# Download check
		##self.statusbar = IDE_customstatusbar.IDEStatusBar(self)
		##self.SetStatusBar(self.statusbar)
		##self.autoupdater = IDE_autodownload.AutoDownloader(self,self.statusbar.GetGauge(),self.statusbar)

		# Keyboard shortcuts
		self.Bind(wx.EVT_KEY_UP, self.OnKeyUp)

	def SubversionLog(self, log):
		self.subversionlog.SetValue(self.subversionlog.GetValue() + log + "\n")
		#self.subversionlog.Refresh()
		self.subversionlog.ShowPosition(self.subversionlog.GetLastPosition())
		self.subversionlog.Update()

################################# PROJECT FUNCTIONS ###########################################

	def OnAddNewScript(self, event):
		print("New Script")
		return 1

	def OnAddExistingScript(self, event):
		print("Existing Script")
		return 1

	def SaveProject(self, event):
		return 0

	def CloseProject(self, event):
		return 0

	def OnQuit(self, event):
		self.Close()

	def OnQuitX(self, event):
		if self.projectman.ProjectLoaded:
			self.projectman.CloseProject()
			if self.projectman.ProjectLoaded:
				event.Veto(True)
			else:
				self.Destroy()
		else:
			self.Destroy()

	def OnKeyUp(self, event):
		if (event.GetKeyCode() > 256):
			return
		if chr(event.GetKeyCode()) == "S" and event.ControlDown():
			if self.notebook.GetPageCount() > 0:
				self.projectman.SaveFile()

class MyApp(wx.App):
	def OnInit(self):
		global frame
		frame = MainWindow(None, -1, 'SubPython')
		frame.Show(True)
		return True

# First check to see if there are any patches into todopatches
app = MyApp(0)
app.MainLoop()
